package com.mpyy.handler.everyday;

import com.mpyy.pojo.*;
import com.mpyy.service.everyday.FinanceserviceInter;
import com.mpyy.service.everyday.ProductServiceInter;
import com.mpyy.service.everyday.PurchaseServiceInter;
import com.mpyy.service.everyday.SupplierServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @Description ProductController
 * Author yyjc
 * @Date 2022-03-06 22:03
 */
@RestController
public class ProductController {
    @Autowired
    private ProductServiceInter productServiceInter;
    @Autowired
    @Qualifier("supplierService")
    private SupplierServiceInter supplierServiceInter;
    @Autowired
    private PurchaseServiceInter purchaseServiceInter;
    @Autowired
    private FinanceserviceInter financeserviceInter;
    @GetMapping("layuipro")
    @ResponseBody
    public Layui getLayui(String layuiid , ProductInfo productInfo){
        if (null==layuiid){
            layuiid="000";
        }
        List<ProductInfo> list=productServiceInter.getproId(layuiid,1,1000000);
        List<ProductInfo> last=productServiceInter.getproId(layuiid,productInfo.getPage(),productInfo.getLimit());
        Layui layui=new Layui();
        layui.setData(last);
        layui.setCount(list.size());
        return layui;
    }
    @ResponseBody
    @GetMapping("tree")
    public List<Tree> getLayuitree(){
        List<Tree> list=productServiceInter.all();
        return list;
    }
    @ResponseBody
    @GetMapping("supplier")
    public Layui supplierController(String supName){
        List<SupplierInfo> supperlierall = supplierServiceInter.supperlierall(supName);
        Layui layui=new Layui();
        layui.setData(supperlierall);
        layui.setCount(supperlierall.size());
        return layui;
    }
    @GetMapping("purchase")
    @ResponseBody
    public Layui purchaseController(String supid){
        List<PurchaseStock> purchaseall = purchaseServiceInter.purchaseall(supid);
        Layui layui=new Layui();
        layui.setData(purchaseall);
        layui.setCount(purchaseall.size());
        return layui;
    }
    @GetMapping("differ")
    @ResponseBody
    public List<PurchaseStock> differController(String supid){
        List<PurchaseStock> differs = supplierServiceInter.differs(supid);
        return differs;
    }
    @GetMapping("detailed")
    @ResponseBody
    public Layui alldetailedController(String purStoId){
        List<PurchaseStock> alldetailed = supplierServiceInter.alldetailed(purStoId);
        Layui layui=new Layui();
        layui.setData(alldetailed);
        layui.setCount(alldetailed.size());
        return layui;
    }
    @GetMapping("insertsupper")
    @ResponseBody
    public Layui insertsupper(String supName, String supPeople, String supPhone, String supAddress){
        List<SupplierInfo> s = supplierServiceInter.supperlierall("s");
        String supplierInfo = s.get(s.size() - 1).getSupId().substring(3,6);
        int a= Integer.parseInt(supplierInfo);
        SupplierInfo supplierobj = new SupplierInfo();
        String supId = null;
        if (a<9){
            supId="sup00"+(a+1);
        }else if (a>=9&&a<99){
            supId="sup0"+(a+1);
        }else if (a>=99){
            supId="sup"+(a+1);
        }
        supplierobj.setSupId(supId);
        supplierobj.setSupName(supName);
        supplierobj.setSupPeople(supPeople);
        supplierobj.setSupPhone(supPhone);
        supplierobj.setSupAddress(supAddress);
        int supperlierinsert = supplierServiceInter.supperlierinsert(supplierobj);
        return supplierController("s");
    }
    @GetMapping("delsupperlier")
    @ResponseBody
    public Layui delsupperlier(String supId){
        int supperlierdel = supplierServiceInter.supperlierdel(supId);
        return supplierController("s");
    }


    @RequestMapping("/updata")
    @ResponseBody
    public Layui supperlierupdata(String supId, String supName, String supPeople , String supPhone, String supAddress ){
        SupplierInfo supplierInfo=new SupplierInfo();
        supplierInfo.setSupId(supId);
        supplierInfo.setSupName(supName);
        supplierInfo.setSupPhone(supPhone);
        supplierInfo.setSupPeople(supPeople);
        supplierInfo.setSupAddress(supAddress);
        int supperlierupdata = supplierServiceInter.supperlierupdata(supplierInfo);
        return supplierController("s");
    }
    @RequestMapping("/finance")
    @ResponseBody
    public Layui financecolstrller(){
        List<AdvancePayment> all = financeserviceInter.all();
        Layui layui=new Layui();
        layui.setCount(all.size());
        layui.setData(all);
        return layui;
    }
    @RequestMapping("/empl")
    @ResponseBody
    public Layui emplcolstrller(String empName, String start, String end){
        if (""==start){
            start="0000-01-01";
        }
        if (""==end){
            end="9999-12-31";
        }
        List<AdvancePayment> empl = financeserviceInter.empl(empName,start,end);
        Layui layui=new Layui();
        layui.setCount(empl.size());
        layui.setData(empl);
        return layui;
    }
    @RequestMapping("/bank")
    @ResponseBody
    public Layui bank(String empName, String start, String end){
        if (""==start){
            start="0000-01-01";
        }
        if (""==end){
            end="9999-12-31";
        }
        List<AdvancePayment> bank = financeserviceInter.bank(empName,start,end);
        List<AdvancePayment> purchaseReturns = financeserviceInter.purReturn(empName, start, end);
        List list=new ArrayList();
        AdvancePayment advancePayment=null;
        for (int i=0;i<bank.size();i++){
            String purStoIds = bank.get(i).getPurStoId();
            advancePayment = bank.get(i);
            advancePayment.setAdvType("支出");
            list.add(advancePayment);
        }
        for (int i=0;i<purchaseReturns.size();i++){
            String purStoId = purchaseReturns.get(i).getPurStoId();
            advancePayment = purchaseReturns.get(i);
            advancePayment.setAdvType("退款");
            list.add(advancePayment);
        }
        purchaseReturns.stream().sequential().collect(Collectors.toCollection(()->list));
        Layui layui=new Layui();
        layui.setCount(bank.size());
        layui.setData(list);
        return layui;
    }
    @RequestMapping("/ghs")
    @ResponseBody
    public Layui ghs(){
        List<SupplierInfo> ghs = supplierServiceInter.ghs();
        List<SupplierInfo> kh = supplierServiceInter.kh();
        ghs.stream().sequential().collect(Collectors.toCollection(()->kh));
        Layui layui=new Layui();
        layui.setCount(ghs.size()+kh.size());
        layui.setData(kh);
        return layui;
    }
    @RequestMapping("/adv")
    @ResponseBody
    public Layui adv(){
        List<AdvancePayment> adv = financeserviceInter.adv();
        Layui layui=new Layui();
        layui.setCount(adv.size());
        layui.setData(adv);
        return layui;
    }
    @RequestMapping("/gu")
    @ResponseBody
    public Layui gu(){
        List<AdvancePayment> gu = financeserviceInter.gu();
        Layui layui=new Layui();
        layui.setCount(gu.size());
        layui.setData(gu);
        return layui;
    }
    @RequestMapping("/insertbank")
    @ResponseBody
    public Layui insertbank(String capital, String accName){
        int insertbank = financeserviceInter.insertbank(capital, accName);
        return financecolstrller();
    }
    @RequestMapping("/reducebank")
    @ResponseBody
    public Layui reducebank(String capital,String accName){
        int insertbank = financeserviceInter.reducebank(capital, accName);
        return financecolstrller();
    }
}
